<template>
    <div style="display: flex;flex-direction: column;background-color: bisque;">
        <span>ai-origin-flux</span>

        <div>
            输入问题
            <input type="text" v-model="question"></input>
            <button @click="handleStreamAiOriginFlux">aiFlux</button>
            <button @click="showReason=!showReason">显隐思考内容</button>
            <hr/>
            <input type="text" v-model="orderId"></input>
            <button @click="handleStreamEpolicyCompare">保单对比</button>
        </div>
        <ai-res-preview 
        :reasoning-content="reasoningContent" 
        :content="content" 
        :show-reason="showReason"></ai-res-preview>
        <hr/>

    </div>
</template>
<script setup lang="ts">

import { URL_STREAM_AI_ORIGIN_FLUX,URL_EPOLICY_COMPARE } from '@/api/ai';
import { sseRequestGet,sseRequestPost } from '@/util/sse-request';
import { ref } from 'vue';

import aiResPreview from '@/components/ai/ai-res-preview.vue';

// 问题与答案
const question = ref('')
const orderId=ref('')

// 展示内容
const reasoningContent = ref('')
const content = ref('')
const showReason=ref(true)


function handleStreamAiOriginFlux() {
    reasoningContent.value=''
    content.value = ''
    sseRequestGet(URL_STREAM_AI_ORIGIN_FLUX + '?prompt=' + question.value, item => {
        // console.log(item)
        const itemData=item.data
        try{
            const itemJson = JSON.parse(itemData)
            const resMsg = itemJson.output.choices[0].message
            if (resMsg.reasoning_content) {
                reasoningContent.value += resMsg.reasoning_content
            }
            if (resMsg.content) {
                content.value += resMsg.content
            }
        }catch(e){
            content.value=itemData
        }

    },()=>{
        content.value+='<br>以上'
    })
}

function handleStreamEpolicyCompare(){
    if(!orderId.value){
        alert('orderId不存在')
        return
    }

    reasoningContent.value=''
    content.value=''
    sseRequestPost(URL_EPOLICY_COMPARE+orderId.value,null,item => {
        // console.log(item)
        const itemData=item.data
        try{
            const itemJson = JSON.parse(itemData)
            // console.log('itemJson:',itemJson)
            const resMsg = itemJson.output.choices[0].message
            // console.log('resMsg:',resMsg)
            if (resMsg.reasoning_content) {
                console.log(resMsg.reasoning_content)
                reasoningContent.value += resMsg.reasoning_content
            }
            if (resMsg.content) {
                console.log(resMsg.content)
                content.value += resMsg.content
            }
        }catch(e){
            console.error(e)
            content.value+=itemData
        }

    },()=>{
        content.value+='<br>以上'
    })
    
}


</script>